package
{
	import EventDispatchers.ImageLoader;
	import EventDispatchers.XMLLoader;
	
	import Events.ImageEvent;
	import Events.XMLEvent;
	
	import Graphics.TileSheet;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class Level
	{
		private var game:Game;
		private var tileSheet:TileSheet;
		private var id:int;
		
		private var world:Array = new Array();
		private var rows:int;
		private var columns:int;
		private var tileSize:int;
		
		private var width:int;
		private var height:int;
		private var numberOfRooms:int;
		
		public function get GameLevel():Game{return game;}
		public function get Width():int {return width;}
		public function get Height():int {return height;}
		public function get TileSet():TileSheet {return tileSheet;}
		public function get World():Array {return world;}
		public function get Rows():int {return rows*numberOfRooms;}
		public function get Columns():int {return columns*numberOfRooms;}
		public function get TileSize():int {return tileSize;}
		public function get NumberOfRooms():int {return numberOfRooms;}
		
		public function Level(game:Game, xml:String, id:int)
		{
			this.game = game;
			this.id = id;
			LoadXML(xml);
		}
		
		private function LoadXML(xml:String):void
		{
			var loader:XMLLoader = new XMLLoader();
			loader.addEventListener(XMLEvent.XMLLOADED, levelInfo);
			loader.load(xml);
		}
		
		private function LoadTileMap(xml:String):void
		{
			var loader:ImageLoader = new ImageLoader();
			loader.addEventListener(ImageEvent.IMAGELOADED, complete);
			loader.Load(xml);
		}
		
		private function LoadLevel(xml:String):void
		{
			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, loading);
			loader.load(new URLRequest(xml));
		}
		
		private function complete(e:ImageEvent):void
		{
			var bitmap:Bitmap = e.Data;
			tileSheet = new TileSheet(bitmap, tileSize,tileSize);
		}
		
		private function loading(e:Event):void
		{		
			for each(var s:String in e.target.data.split("\n"))
			{
				var temp:Array = s.split(",");
				world.push(temp);       
			}
			unitPlacement();
		}
		
		private function levelInfo(e:XMLEvent):void
		{
			var xml:XML = new XML(e.Data);
			LoadTileMap(xml.tileSheet.toString());
			LoadLevel(xml.map.toString());
			this.rows = (int)(xml.rows.toString());
			this.columns = (int)(xml.columns.toString());
			this.tileSize = (int)(xml.tileSize.toString());
			this.numberOfRooms = (int)(xml.numberOfRooms.toString());
			this.width = rows * tileSize * numberOfRooms;
			this.height = columns * tileSize * numberOfRooms;
			
		}
		
		private function unitPlacement():void
		{
			for(var row:int = 0; row < world.length; row++)
			{
				for(var column:int = 0; column < world[row].length; column++)
				{
					if(world[row][column] == 101)
					{
						game.Hero.setPosition(column, row);
					}
				}
			}
		}
	}
}